import nyNetwork from "@/api/nyBaseNetwork";
import DZModelPage from "@/model/DZModelPage";
import uploadFile from "@/utils/DZUploadFile";
import nyMessageToast from "@/utils/NYMessageToast";
import DZModelArticle from "../model/DZModelArticle";
import DZModelType from "../model/DZModelType";
import DZModelWorks from "../model/DZModelWorks";

class DZServiceArticle {

    m_page = new DZModelPage(10);

    list_type: Array<DZModelType> = [];

    m_type = new DZModelType();

    list_article: Array<DZModelArticle> = [];

    m_article = new DZModelArticle();

    list_works: Array<DZModelWorks> = [];

    m_works = new DZModelWorks();

    net_add_type(success: () => void) {
        nyNetwork.net("添加分类", { ...this.m_type }, data => {
            this.m_type = new DZModelType();
            success();
        })
    }

    net_edit_type(success: () => void) {
        nyNetwork.net("修改分类", { ...this.m_type }, data => {
            this.m_type = new DZModelType();
            success();
        })
    }

    net_del_type(success: () => void) {
        nyNetwork.net("删除分类", { id: this.m_type.id }, data => {
            success();
        })
    }

    net_list_type(cur_page) {
        var arr_data = [];
        nyNetwork.net("分类列表", { page: cur_page }, data => {
            this.m_page = new DZModelPage(10);
            this.m_page.page = cur_page;
            this.m_page.total = data.total;
            for (const item of data.list) {
                var type = new DZModelType();
                type.id = item.iId ?? '';
                type.logo = item.iLogoId + '' ?? '';
                if (item.iLogoId) {
                    type.logo_url = nyNetwork.image(item.iLogoId)
                }
                type.name = item.sName ?? '';
                type.sort = item.iSort ?? 0;
                arr_data.push(type);
            }
            this.list_type = arr_data;
        })
    }

    net_list_type_dic() {
        var arr_data = [];
        nyNetwork.net("分类字典列表", {}, data => {
            for (const item of data) {
                var type = new DZModelType();
                type.id = item.iId ?? '';
                type.name = item.sName ?? '';
                arr_data.push(type);
            }
            this.list_type = arr_data;
        })
    }

    net_upload_image(file, success: (data) => void, filed: () => void) {
        uploadFile.uploadImage(file,
            (res) => {
            }, (res) => {
                success(res + '');
                nyMessageToast.success("图片上传成功")
            }, (res) => {
                filed();
                nyMessageToast.error("图片上传失败")
            });
    }

    net_add_article(success: () => void) {
        if (!this.m_article.typeId) {
            nyMessageToast.warning("请选择文章分类")
            return;
        }

        if (this.m_article.title.nyIsEmpty()) {
            nyMessageToast.warning("请输入文章标题")
            return;
        }

        if (this.m_article.des.nyIsEmpty()) {
            nyMessageToast.warning("请输入文章描述")
            return;
        }

        if (!this.m_article.listCover) {
            nyMessageToast.warning("请上传列表封面")
            return;
        }

        if (!this.m_article.bannerCover) {
            nyMessageToast.warning("请上传内容图片")
            return;
        }

        if (this.m_article.content.nyIsEmpty() && this.list_works.length == 0) {
            nyMessageToast.warning("没有设置文章内容")
            return;
        }

        nyNetwork.net("添加文章", { ...this.m_article, worksList: JSON.stringify(this.list_works) }, data => {
            success();
        })
    }

    net_del_article(id, success: () => void) {
        nyNetwork.net("删除文章", { id: id }, data => {
            success();
        })
    }

    net_edit_article(success: () => void) {
        console.log(this.m_article)
        if (this.m_article.typeId == '') {
            nyMessageToast.warning("请选择文章分类")
            return;
        }

        if (this.m_article.title.nyIsEmpty()) {
            nyMessageToast.warning("请输入文章标题")
            return;
        }

        if (this.m_article.des.nyIsEmpty()) {
            nyMessageToast.warning("请输入文章描述")
            return;
        }

        if (!this.m_article.listCover) {
            nyMessageToast.warning("请上传列表封面")
            return;
        }

        if (!this.m_article.bannerCover) {
            nyMessageToast.warning("请上传内容图片")
            return;
        }

        if (this.m_article.content.nyIsEmpty() && this.list_works.length == 0) {
            nyMessageToast.warning("没有设置文章内容")
            return;
        }

        nyNetwork.net("编辑文章", { ...this.m_article }, data => {
            success();
        })
    }

    net_list_article(cur_page) {
        var arr_data = [];
        nyNetwork.net("文章列表", { typeId: this.m_article.typeId, name: this.m_article.title, page: cur_page }, data => {
            this.m_page = new DZModelPage(10);
            this.m_page.page = cur_page;
            this.m_page.total = data.total;
            for (const item of data.list) {
                var article = new DZModelArticle();
                article.id = item.iId ?? '';
                article.listCover = item.iListId;
                if (item.iListId) {
                    article.listCoverUrl = nyNetwork.image(item.iListId);
                }
                article.title = item.sTitle ?? '';
                article.des = item.sDes ?? '';
                article.typeName = item.sTypeName ?? '';
                article.sort = item.iSort ?? 0;
                article.top = item.iTop ?? 0;
                arr_data.push(article)
            }
            this.list_article = arr_data;
        })
    }

    net_list_article_all() {
        var arr_data = [];
        nyNetwork.net("所有文章", {}, data => {
            for (const item of data) {
                var article = new DZModelArticle();
                article.id = item.iId ?? '';
                article.title = item.sTitle ?? '';
                arr_data.push(article)
            }
            this.list_article = arr_data;
        })
    }

    net_details_article() {
        nyNetwork.net("文章详情", { id: this.m_article.id }, data => {
            this.m_article.parse(data);
            var arr_data = [];
            for (const item of data.worksModelList) {
                var works = new DZModelWorks();
                works.iId = item.iId ?? '';
                works.iBannerId = item.iBannerId ?? '';
                if (item.iBannerId) {
                    works.bannerUrl = nyNetwork.image(item.iBannerId);
                }
                works.sName = item.sName ?? '';
                works.sAuthor = item.sAuthor ?? '';
                works.sCompanyName = item.sCompanyName ?? '';
                works.sWxId = item.sWxId ?? '';
                works.sVideoId = item.sVideoId ?? '';
                arr_data.push(works)
            }
            this.list_works = arr_data;
        })
    }

    net_add_works(success: () => void) {
        nyNetwork.net("添加视频号", { ...this.m_works, articleId: this.m_article.id }, data => {
            this.m_works = new DZModelWorks();
            success();
        })
    }

    net_del_works(success: () => void) {
        nyNetwork.net("删除视频号", { id: this.m_works.iId }, data => {
            success();
        })
    }

    net_move_up(id, success: () => void) {
        nyNetwork.net("数据上移", { id: id }, data => {
            success();
        })
    }
    net_move_down(id, success: () => void) {
        nyNetwork.net("数据下移", { id: id }, data => {
            success();
        })
    }
    net_move_top(id, success: () => void) {
        nyNetwork.net("数据置顶", { id: id }, data => {
            success();
        })
    }
    net_cancel_top(id, success: () => void) {
        nyNetwork.net("取消置顶", { id: id }, data => {
            success();
        })
    }
}
export default DZServiceArticle;